<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2021/5/26
 * Time: 15:40
 */

namespace App\Services;

use Illuminate\Support\Facades\Storage;

class FileService
{

    public function responseStream($path, $action = 'download', $filename = '')
    {
        $filename = $this->getFilename($filename, $path);
        $storage = Storage::disk(config('admin.upload.disk'));
        try {
            if ($action == 'show') {
                return $storage->response($path, $filename);
            } else {
                return $storage->download($path, $filename);
            }
        } catch (\Exception $e) {
        }
        return response()->view('errors.404', [], 404);
    }

    protected function getFilename($filename, $path): string
    {
        return valid_download_filename($filename, $path);
    }
}
